package jp.co.cos.ittrick.channel.controller.client;

import java.util.List;

import jp.co.cos.ittrick.channel.model.ChapterModel;
import jp.co.cos.ittrick.channel.model.PageModel;
import jp.co.cos.ittrick.channel.service.ChapterService;
import jp.co.cos.ittrick.channel.service.PageService;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;

public class DeleteChapterController extends Controller {

    private ChapterService chapterService;
    private PageService pageService;

    public DeleteChapterController() {
        chapterService = new ChapterService();
        pageService = new PageService();
    }

    @Override
    public Navigation run() throws Exception {

        // リクエストパラメータ
        String chapter = asString("chapter");

        // Pageの削除
        ChapterModel chapterModel = chapterService.getChapterByKey(Integer.valueOf(chapter));
        List<PageModel> pageList = chapterModel.getPageModelListRef().getModelList();
        for(PageModel pageModel: pageList) {
            pageService.delete((int)pageModel.getKeyString());
        }

        // Delete
        chapterService.delete(Integer.valueOf(chapter));

        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html");
        response.getWriter().println("0");
        response.flushBuffer();

        return null;
    }
}
